package com.huxiaohui.ssbx.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolTaskExecutor sendMessageExecutor(){
        ThreadPoolTaskExecutor sendMessageExecutor = new ThreadPoolTaskExecutor();
        //配置核心线程数
        int N = Runtime.getRuntime().availableProcessors();
        sendMessageExecutor.setCorePoolSize(N * 2);
        //配置最大线程数
        sendMessageExecutor.setMaxPoolSize(N * 4);
        // 最大线程数过期时间
        sendMessageExecutor.setKeepAliveSeconds(60);
        //配置队列大小
        sendMessageExecutor.setQueueCapacity(1000);
        //配置线程池中的线程的名称前缀
        sendMessageExecutor.setThreadNamePrefix("send-message-");
        // 由调用者线程执行
        sendMessageExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        sendMessageExecutor.initialize();
        return sendMessageExecutor;
    }
}
